<template>
  <div class="mod-config">
    <el-form
      :inline="true"
      :model="dataForm"
      @keyup.enter.native="getDataList()"
    >
      <el-form-item>
        <el-input
          v-model="dataForm.key"
          placeholder="参数名"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button
          type="danger"
          @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0"
          >批量删除</el-button
        >
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%"
    >
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50"
      >
      </el-table-column>
      <el-table-column
        prop="horderId"
        header-align="center"
        align="center"
        label="砍价订单id"
      >
      </el-table-column>
      <el-table-column
        prop="goodsName"
        header-align="center"
        align="center"
        label="商品名称"
      >
      </el-table-column>
      <el-table-column
        prop="goodsImg"
        header-align="center"
        align="center"
        label="商品图片"
      >
        <template slot-scope="scope">
          <img :src="scope.row.goodsImg" width="150px" />
        </template>
      </el-table-column>
      <el-table-column
        prop="horderAdd"
        header-align="center"
        align="center"
        label="收货地址"
      >
      </el-table-column>
      <el-table-column
        prop="goodsCount"
        header-align="center"
        align="center"
        label="商品数量"
      >
      </el-table-column>
      <el-table-column
        prop="goodsPrice"
        header-align="center"
        align="center"
        label="商品原价"
      >
      </el-table-column>
      <el-table-column
        prop="horderYprice"
        header-align="center"
        align="center"
        label="已砍"
      >
      </el-table-column>
      <el-table-column
        prop="horderPrice"
        header-align="center"
        align="center"
        label="应支付价格"
      >
      </el-table-column>
      <el-table-column
        prop="horderStatus"
        header-align="center"
        align="center"
        label="订单状态"
      >
      </el-table-column>
      <el-table-column
        prop="payDate"
        header-align="center"
        align="center"
        label="支付时间"
      >
      </el-table-column>
      <el-table-column
        prop="createDate"
        header-align="center"
        align="center"
        label="订单创建时间"
      >
      </el-table-column>
      <el-table-column
        prop="userId"
        header-align="center"
        align="center"
        label="用户id"
      >
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="cut(scope.row)"
            >砍一刀</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>

    <el-dialog title="请登录" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="from">
        <el-form-item label="用户名" :label-width="formLabelWidth">
          <el-input v-model="form.userName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth">
          <el-input v-model="form.userPwd" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="cutone()">砍一刀</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
<script>
import AddOrUpdate from "./bargainorder-add-or-update";
function getQueryString(name) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  console.log(reg + "****************");
  var r = window.location.href.substr(1).match(reg);
  console.log(r + "+++++++++++++++");
  if (r != null) {
    return decodeURIComponent(r[2]);
  }
  return "";
}
function getQueryString1(name) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  console.log(reg + "****************");
  var r = window.location.href.substr(1).match(reg);
  console.log(r + "+++++++++++++++");
  if (r != null) {
    return decodeURIComponent(r[2]);
  }
  return "";
}

export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入用户名/手机号"));
      } else {
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        callback();
      }
    };
    return {
      rules: {
        userName: [{ validator: validatePass, trigger: "blur" }],
        userPwd: [{ validator: validatePass2, trigger: "blur" }],
      },
      url: "",
      dialogVisible: false,
      dataForm: {
        key: "",
        horderId: 0,
        activityHid: "",
        goodsId: "",
        goodsName: "",
        horderAdd: "",
        goodsCount: "",
        goodsPrice: "",
        horderYprice: "",
        horderPrice: "",
        horderStatus: "",
        payDate: "",
        createDate: "",
        goodsImg:'',
        userId: "",
      },
      userid: "",
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      baseUrl: "http://localhost:88/api",
      dialogFormVisible: false,
      form: {
        userName: "",
        userPwd: "",
      },
      formLabelWidth: "120px",
    };
  },
  components: {
    AddOrUpdate,
  },

  created() {
    let userid = getQueryString("userid");
    let bargainId = getQueryString1("bargainId");
    console.log(bargainId + "--------------");
    console.log(userid + "--------------");
    this.userid = userid;
    this.getDataList(userid, bargainId);
  },
  methods: {
    //登录成功可以砍价
    cutone() {
      this.$axios
        .post(`${this.baseUrl}/pms/user/login`, this.form)
        .then(({ data }) => {
          console.log(data.user);
          if (data && data.code === 0) {
            localStorage.setItem("userId", data.user.userId);
            localStorage.setItem("userName", data.user.userName);
            this.dataForm.userId = data.user.userId;
            this.$axios
              .post(
                `${this.baseUrl}/kms/bargainorder/cut/${this.userid}`,
                this.dataForm
              )
              .then(({ data }) => {
                if (data && data.code === 0) {
                  alert("砍价成功");
                  this.$router.push("/activity");
                } else {
                  alert("登录失败");
                  this.$router.push("/register");
                }
                this.dataListLoading = false;
              });
          } else {
            alert("登录失败");
            this.$router.push("/register");
          }
          this.dataListLoading = false;
        });
      alert("submit!");
    },
    //砍一刀去登录
    cut(scope) {
      console.log(scope,'===================')
      this.dialogFormVisible = true;
      this.dataForm.activityHid=scope.activityHid;
      this.dataForm.goodsId=scope.goodsId;
      this.dataForm.horderId=scope.horderId,
      this.dataForm.goodsName=scope.goodsName,
      this.dataForm.goodsPrice=scope.goodsPrice
      this.dataForm.goodsImg=scope.goodsImg
      this.dataForm.horderPrice=scope.horderPrice
    },
    // 获取数据列表
    getDataList(userid, bargainId) {
      this.dataListLoading = true;
      this.$axios
        .get(
          `${this.baseUrl}/kms/bargainorder/find?userid=${userid}&&bargainId=${bargainId}`
        )
        .then(({ data }) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list;
            this.totalPage = data.page.totalCount;
          } else {
            this.dataList = [];
            this.totalPage = 0;
          }
          this.dataListLoading = false;
        });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map((item) => {
            return item.horderId;
          });
      this.$confirm(
        `确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(() => {
        this.$http({
          url: this.$http.adornUrl("/kms/bargainorder/delete"),
          method: "post",
          data: this.$http.adornData(ids, false),
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              },
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    },
  },
};
</script>
  